package com.kye.express.business.waybill_take_photo.history;

import android.content.Context;
import android.text.TextUtils;

import com.kye.express.R;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.RxView;
import com.kye.express.utils.DateTimeUtils;
import com.kye.express.utils.StringUtils;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.online.GetPayRecordResultBean;
import com.kye.model.bean.online.request.RequestGetPayRecordBean;
import com.kye.model.bean.user.User;
import com.kye.model.bean.waybill_take_photo.QueryWaybillImageResultBean;
import com.kye.model.bean.waybill_take_photo.QueryWaybillTakePhotoHistoryResultBean;
import com.kye.model.bean.waybill_take_photo.RequestQueryWaybillImageBean;
import com.kye.model.bean.waybill_take_photo.RequestQueryWaybillTakePhotoHistoryBean;
import com.kye.model.constant.Constants;
import com.kye.model.service.OnlinePayService;
import com.kye.model.service.WaybillTakePhotoService;

import java.util.Calendar;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public interface WaybillTakePhotoHistoryActivityContract {

    interface WaybillTakePhotoHistoryActivityView extends RxView {

        void onComplete(boolean isLoadMore);

        void onLoading();

        void onLoaded(QueryWaybillTakePhotoHistoryResultBean datas, boolean isLoadMore);

        void onError(String msg, boolean isLoadMore);

        void onError(String msg);

        void onComplete();

        void onLoadedImage(QueryWaybillImageResultBean datas);
    }

    class WaybillTakePhotoHistoryActivityViewPresenter extends BasePresenter<WaybillTakePhotoHistoryActivityContract.WaybillTakePhotoHistoryActivityView> {

        private int mCurrentPage = 1;
        private Context mContext;

        public WaybillTakePhotoHistoryActivityViewPresenter(Context context) {
            this.mContext = context;
        }

        /**
         * 请求服务器数据
         *
         * @param isFirstIn  是否是刚进入页面请求数据，刚进入页面就需要显示loading view
         * @param isLoadMore true：加载下一页，false：刷新页面，加载第一页
         */
        public void loadDatas(final boolean isFirstIn, final boolean isLoadMore) {
            String phone = "";
            User user = UserSession.getInstance().getUser();
            if (user != null) {
                phone = user.getPhone();
            }
            String startTime = DateTimeUtils.getStringDateByMounthDay(getLastTime(1));
            String endTime = DateTimeUtils.getStringDateByMounthDay(System.currentTimeMillis());

            RetrofitManager.getInstance().createOpenApiService(WaybillTakePhotoService.class)
                    .queryWaybillTakephotoHistory(new RequestQueryWaybillTakePhotoHistoryBean(phone, startTime, endTime, (isLoadMore ? (mCurrentPage + 1) : 1), Constants.PAGE_SIZE, 2))
                    .compose(getView().<OpenApiResult<QueryWaybillTakePhotoHistoryResultBean>>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe((new OpenApiResultObserver<QueryWaybillTakePhotoHistoryResultBean>() {

                        @Override
                        public void onLoading(Disposable d) {
                            if (isFirstIn) {
                                getView().onLoading();
                            }
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onError(mContext.getString(R.string.common_net_error), isLoadMore);
                        }

                        @Override
                        public void onLoaded() {
                            getView().onComplete(isLoadMore);
                        }

                        @Override
                        public void onSuccess(OpenApiResult<QueryWaybillTakePhotoHistoryResultBean> result, QueryWaybillTakePhotoHistoryResultBean datas) {
                            if (!isLoadMore) {
                                resetPage();
                            }

                            if (datas != null) {
                                getView().onLoaded(datas, isLoadMore);
                                if (isLoadMore) {
                                    setCurrentPage(mCurrentPage + 1);
                                }
                            } else {
                                getView().onError(mContext.getString(R.string.common_no_more_datas), isLoadMore);
                            }
                        }

                        @Override
                        public void onApiError(OpenApiResult<QueryWaybillTakePhotoHistoryResultBean> result, int code, String msg) {
                            if (!TextUtils.isEmpty(msg)) {
                                getView().onError(msg, isLoadMore);
                            } else {
                                getView().onError(mContext.getString(R.string.common_net_error), isLoadMore);
                            }
                        }
                    }));
        }

        public void getPicture(String waybill) {
            String phone = "";
            User user = UserSession.getInstance().getUser();
            if (user != null) {
                phone = user.getPhone();
            }

            RetrofitManager.getInstance().createOpenApiService(WaybillTakePhotoService.class)
                    .queryWaybillImage(new RequestQueryWaybillImageBean(phone, waybill))
                    .compose(getView().<OpenApiResult<QueryWaybillImageResultBean>>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe((new OpenApiResultObserver<QueryWaybillImageResultBean>() {

                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onError(mContext.getString(R.string.common_net_error));
                        }

                        @Override
                        public void onLoaded() {
                            getView().onComplete();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<QueryWaybillImageResultBean> result, QueryWaybillImageResultBean datas) {
                            if (datas != null) {
                                getView().onLoadedImage(datas);
                            } else {
                                getView().onError(mContext.getString(R.string.common_no_more_datas));
                            }
                        }

                        @Override
                        public void onApiError(OpenApiResult<QueryWaybillImageResultBean> result, int code, String msg) {
                            if (!TextUtils.isEmpty(msg)) {
                                getView().onError(msg);
                            } else {
                                getView().onError(mContext.getString(R.string.common_net_error));
                            }
                        }
                    }));
        }

        public int getCurrentPage() {
            return mCurrentPage;
        }

        public void setCurrentPage(int currentPage) {
            this.mCurrentPage = currentPage;
        }

        public void resetPage() {
            setCurrentPage(1);
        }

        /**
         * 获取过去几个月的时间
         * l
         *
         * @param lastMoth 1表示1个月前
         * @return
         */
        private long getLastTime(int lastMoth) {
            Calendar mCurrentT = Calendar.getInstance();
            //1个月零一周以前的时间
            mCurrentT.set(Calendar.MONTH, mCurrentT.get(Calendar.MONTH) - lastMoth);
            return mCurrentT.getTimeInMillis();
        }
    }
}
